<?php

namespace backend\controllers;

/**
 * @author  shuang
 * @date    2017-1-2 21:48:43
 * @version V1.0
 * @desc    
 */
use common\components\BController;
use common\components\MTools;
use yii\helpers\Json;
use Yii;
use backend\models\MY_Task;
use yii\helpers\ArrayHelper;

class MerchantController extends BController {

    public $flag = false;
    public $message;

    public function actions() {
        return [
            "tasklist" => [
                "class" => "\common\actions\ListAction",
                "modelClass" => new MY_Task(),
                "renderTo" => "tasklist"
            ]
        ];
    }

    /*
     * ajax数据列表
     */

    public function actionAjaxtasklist() {
        $res = MY_Task::getList();
        $temp = ["total" => $res["total"], "rows" => []];
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $action = MTools::getStringActions([
                            "taskdetail" => [
                                "options" => ["onclick" => "javascript:taskmanager.taskdetail(" . $item["id"] . ");"],
                                "title" => "查看详情"
                            ],
                            "stoptask" => [
                                "options" => ["onclick" => "javascript:taskmanager.stoptask(" . $item["id"] . ");"],
                                "title" => "终止任务"
                            ],
                                ], [ "taskdetail" => ["show" => true, "url" => "javascript:;"], "stoptask" => ["show" => true, "url" => "javascript:;"], "cashbacktask" => ["show" => true, "url" => "javascript:;"]]);
                $temp["rows"][] = [
                    "id" => $item["id"],
                    "title" => Yii::$app->formatter->asText($item["title"]),
                    "type" => $item["typename"],
                    "stacount" => Yii::$app->formatter->asInteger($item["stacount"] ? $item["stacount"] : 0),
                    "hidcount" => Yii::$app->formatter->asInteger($item["hidcount"] ? $item["hidcount"] : 0),
                    "comcount" => Yii::$app->formatter->asInteger($item["comcount"] ? $item["comcount"] : 0),
                    "number-hidcount" => Yii::$app->formatter->asDecimal($item["number"], 0) . "/" . Yii::$app->formatter->asInteger($item["hidcount"] ? $item["hidcount"] : 0),
                    "single_meet" => Yii::$app->formatter->asDecimal(ArrayHelper::getValue($item, "single_meet"), 0),
                    "username" => $item["username"],
                    "nosheetcount" => Yii::$app->formatter->asInteger($item["nosheetcount"] ? $item["nosheetcount"] : 0),
                    "status" => $item["statusname"],
                    "created_at" => Yii::$app->formatter->asDatetime($item["created_at"]),
                    "action" => $action
                ];
            }
        }
        echo Json::encode($temp);
    }

    /*
     * 查询任务详情
     */

    public function actionTaskdetail() {
        $id = Yii::$app->request->post("id");
        $result = MY_Task::getTaskDetail($id);
        $taskTopcount = \frontend\models\WB_TaskTop::find()->select("id")->count();
        $result["sample_figure"] = MTools::Qiniunow480($result["sample_figure"]);
        $result["qrcode"] = MTools::Qiniunow480($result["qrcode"]);
        $result["tasktopcount"] = MTools::getYiiParams("topcount") - $taskTopcount;
        if ($result) {
            echo Json::encode(["status" => true, "data" => $result]);
        } else {
            echo Json::encode(["status" => false, "message" => "查询任务信息有误，请您确认！"]);
        }
        exit();
    }

    /*
     * 终止任务
     */

    public function actionStoptask() {
        if (Yii::$app->request->isAjax) {
            $taskid = Yii::$app->request->post("id");
            $stopcashback = Yii::$app->request->post("stopcashback");
            $event = new \common\components\TaskEvent();
            $this->on("stop_task", [$event, "stopTask"], [
                "hidid" => $taskid,
                "class_type" => 3,
                "type" => 1,
                "operateMsg" => "系统强制操作：",
                "successnote" => $stopcashback == 2 ? "系统强制操作：任务终止成功，系统不对剩余任务返现" : null
            ]);
            $this->trigger("stop_task", $event);
            $this->off("stop_task");
            if ($stopcashback == 2) {  //终止任务，不返现
                if ($this->flag) {
                    $taskmodel = MY_Task::findOne($taskid);
                    $taskmodel->stopcashback = 2;
                    if (!$taskmodel->save()) {
                        $this->message = "更新状态失败";
                    }
                }
            } else if ($stopcashback == 3) { //终止任务，立即返现
                $this->on("mywork_list", [$event, "QtimingCashback"], [
                    "taskid" => $taskid,
                    "type" => 1,
                    "class_type" => 3,
                    "note" => "系统强制处理停止任务，剩余返现"
                ]);
                $this->trigger("mywork_list", $event);
                $this->off("mywork_list");
            }
            echo Json::encode(["status" => $this->flag, "message" => $this->message]);
        }
    }

    /*
     * 终止返现
     */

    public function actionCashbacktask() {
        if (Yii::$app->request->isAjax) {
            $taskid = Yii::$app->request->post("id");
            $event = new \common\components\TaskEvent();
            $this->on("mywork_list", [$event, "QtimingCashback"], [
                "taskid" => $taskid,
                "type" => 1,
                "class_type" => 3,
                "note" => "系统强制处理停止任务，剩余返现"
            ]);
            $this->trigger("mywork_list", $event);
            $this->off("mywork_list");
            echo Json::encode(["status" => $this->flag, "message" => $this->message]);
        }
    }

    /*
     * 置顶任务
     */

    public function actionToptask() {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        if (Yii::$app->request->isAjax) {
            $taskid = Yii::$app->request->post("id");
            $isdedu = Yii::$app->request->post("isdedu");
            $taskmodel = MY_Task::findOne($taskid);
            $event = new \common\components\TaskEvent();
            $this->on("add_task_top", [$event, "addTaskTopEvent"], [
                "userid" => $taskmodel->userid,
                "taskmoney" => 10,
                "hidid" => $taskid,
                "operateMsg" => "任务置顶",
                "type" => 2,
                "class_type" => 7,
                "note" => "任务置顶：编号" . $taskid,
                "isdedu" => $isdedu > 0 ? false : true
            ]);
            $this->trigger("add_task_top", $event);
            $this->off("add_task_top");
            return ["status" => $this->flag, "message" => $this->message];
        }
    }

    /*
     * 追加任务
     */

    public function actionAddtask() {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        if (Yii::$app->request->isAjax) {
            $taskid = Yii::$app->request->post("id");
            $isdedu = Yii::$app->request->post("isdedu");
            $number = Yii::$app->request->post("number");
            $price = Yii::$app->request->post("price");
            $taskmodel = MY_Task::findOne($taskid);
            $event = new \common\components\TaskEvent();
            $this->on("add_task_number", [$event, "addTaskNumber"], [
                "userid" => $taskmodel->userid,
                "taskmoney" => $number * $price,
                "hidid" => $taskid,
                "number" => $number,
                "operateMsg" => "追加任务数量",
                "type" => 2,
                "class_type" => 6,
                "note" => "追加任务:编号" . $taskid . "消耗",
                "isdedu" => $isdedu > 0 ? false : true
            ]);
            $this->trigger("add_task_number", $event);
            $this->off("add_task_number");
            return ["status" => $this->flag, "message" => $this->message];
        }
    }

    /*
     * 刷新任务
     */

    public function actionReflashtask() {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        if (Yii::$app->request->isAjax) {
            $taskid = Yii::$app->request->post("id");
            $isdedu = Yii::$app->request->post("isdedu");
            $taskmodel = MY_Task::findOne($taskid);
            $event = new \common\components\TaskEvent();
            $this->on("reflash_task", [$event, "reflashTask"], [
                "userid" => $taskmodel->userid,
                "taskmoney" => MTools::getYiiParams("reflash_price") * Yii::$app->request->post("reflashNO"),
                "hidid" => $taskid,
                "reno" => Yii::$app->request->post("reflashNO"),
                "operateMsg" => "刷新",
                "type" => 2,
                "class_type" => 7,
                "auto" => false,
                "note" => "刷新任务：编号" . $taskid,
                "isdedu" => $isdedu > 0 ? false : true
            ]);
            $this->trigger("reflash_task", $event);
            $this->off("reflash_task");
            return ["status" => $this->flag, "message" => $this->message];
        }
    }

    /*
     * 编辑任务
     */

    public function actionEdittask() {
        $taskid = Yii::$app->request->post("id");
        $taskmodel = MY_Task::findOne($taskid);
        $event = new \common\components\TaskEvent();
        $this->on("edit_task", [$event, "editTask2"], [
            "hidid" => $taskid,
            "userid" => $taskmodel->userid,
            "operateMsg" => "编辑任务",
        ]);
        $this->trigger("edit_task", $event);
        $this->off("edit_task");
        Yii::$app->getSession()->setFlash('success', $this->message);
        $this->goBack(\Yii::$app->request->headers['Referer']);
        //$this->redirect(["tasklist"]);
    }

}
